﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using _8_21_Classwork_Cafe.Models;

namespace _8_21_Classwork_Cafe.Controllers
{
    public class HomeController : Controller
    {
        List<CafeShop> shops = Singleton.Instance.Shops;
        public ActionResult Index()
        {
            return View(shops);
        }
        [HttpGet]
        public ActionResult AddCafe()
        {
            return View();
        }
        [HttpPost]
        public ActionResult AddCafe(string cName, string cInfo, string cImage){
            CafeShop cafe = new CafeShop();
            cafe.Name = cName;
            cafe.Info = cInfo;
            cafe.Image = cImage;
            shops.Add(cafe);
            return RedirectToAction("Index");
        }
        public ActionResult Details(int id)
        {
            CafeShop target = shops.Where(x => x.ID == id).FirstOrDefault();
            return View(target);
        }
        [HttpGet]
        public ActionResult AddReview(int id)
        {
            CafeShop target = shops.Where(x => x.ID == id).FirstOrDefault();
            return View(target);
        }
        [HttpPost]
        public ActionResult AddReview(string name, string rev, int rate, int id)
        {
            ReviewStruct review = new ReviewStruct(username:name, userreview:rev, rating:rate);


            CafeShop cafe = shops.Where(x => x.ID == id).FirstOrDefault();
            cafe.reviews.Add(review);
            return RedirectToAction("Details/" + id);
        }
        public ActionResult SortList(int id)
        {
            if (id == 1)
            {
                for (int i = 1; i < shops.Count; i++)
                {
                    for (int j = 0; j < (shops.Count - i); j++)
                    {
                        if (shops[j].AvgRating() < shops[j + 1].AvgRating())
                        {
                            CafeShop temp = shops[j];
                            shops[j] = shops[j + 1];
                            shops[j + 1] = temp;
                        }
                    }
                }
                //sort by highest rated
            }
            else if (id == 2)
            {
                for (int i = 1; i < shops.Count; i++)
                {
                    for (int j = 0; j < (shops.Count - i); j++)
                    {
                        if (shops[j].AvgRating() > shops[j + 1].AvgRating())
                        {
                            CafeShop temp = shops[j];
                            shops[j] = shops[j + 1];
                            shops[j + 1] = temp;
                        }
                    }
                }
                //sort by least rated
            }
            else if (id == 3)
            {
                for (int i = 1; i < shops.Count; i++)
                {
                    for (int j = 0; j < (shops.Count - i); j++)
                    {
                        if (shops[j].reviews.Count < shops[j + 1].reviews.Count)
                        {
                            CafeShop temp = shops[j];
                            shops[j] = shops[j + 1];
                            shops[j + 1] = temp;
                        }
                    }
                }
                //sort by Most Reviews
            }
            else if (id == 4)
            {
                for (int i = 1; i < shops.Count; i++)
                {
                    for (int j = 0; j < (shops.Count - i); j++)
                    {
                        if (shops[j].reviews.Count > shops[j + 1].reviews.Count)
                        {
                            CafeShop temp = shops[j];
                            shops[j] = shops[j + 1];
                            shops[j + 1] = temp;
                        }
                    }
                }
                //sort by least reviews
            }
            else if (id == 5)
            {
                for (int i = 1; i < shops.Count; i++)
                {
                    for (int j = 0; j < (shops.Count - i); j++)
                    {
                        if (shops[j].AvgRating() < shops[j + 1].AvgRating())
                        {
                            CafeShop temp = shops[j];
                            shops[j] = shops[j + 1];
                            shops[j + 1] = temp;
                        }
                    }
                }

                return RedirectToAction("Index2");
                //only display top 5 cafes
            }

            return RedirectToAction("Index");
        }
        public ActionResult Index2(){
            return View(shops);
        }
    }
}